import { Injectable } from '@nestjs/common'
import { PrismaService } from '../prisma/prisma.service'
import { Prisma } from '@prisma/client'

@Injectable()
export class WebCategoryService {
    constructor(private readonly prisma: PrismaService) {}

    async list() {
        const list = await this.prisma.$queryRaw`
            SELECT
            id,
            name,
            (SELECT CAST(COUNT(1) AS REAL) from article WHERE article.categoryId=category.id AND article.status=1) as articleCount 
            FROM category ORDER BY sort ASC
        `
        return list
    }

	async detail (id: number) {
		const detail = await this.prisma.$queryRaw`
            SELECT
            id,
            name,
            (SELECT CAST(COUNT(1) AS REAL) from article WHERE article.categoryId=category.id) as articleCount 
            FROM category WHERE ${Prisma.sql`id=${id}`}
        `

		return detail[0]
	}
}
